package com.boc.pickview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import com.boc.pickview.view.BasePickerView;
import com.boc.pickview.view.WheelOptions;
import com.boc.pickview.view.WheelTime;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 *
 */
public class TimePickerView <T> extends BasePickerView implements View.OnClickListener, WheelOptions.onTimePeriodChangeListener {


    @Override
    public void timePeriodChange(String text) {

        timePeriodChangeListener.timePeriodChange(text);
    }

    public enum Type {
        ALL, YEAR_MONTH_DAY, HOURS_MINS, MONTH_DAY_HOUR_MIN , YEAR_MONTH,MONTH_WEEK_HOUR_MIN
    }// 选择模式，年月日时分，年月日，时分，月日时分,月日周时分

    WheelTime wheelTime;
    WheelOptions wheelOptions;
    private View btnSubmit, btnCancel;
    private TextView tvTitle;
    private static final String TAG_SUBMIT = "submit";
    private static final String TAG_CANCEL = "cancel";
    private OnTimeSelectListener timeSelectListener;
    private onTimePeriodChangeListener timePeriodChangeListener;
    private OnOptionsSelectListener optionsSelectListener;

    private Type mType;

    public void setStartYear(int startYear)
    {

        wheelTime.setStartYear(startYear);
    }

    public void endYear(int endyear)
    {

        wheelTime.setEndYear(endyear);
    }



    public TimePickerView(Context context, Type type) {
        super(context);

        mType = type;
       // LayoutInflater.from(context).inflate(R.layout.pickerview_time, contentContainer);
        if(type == Type.MONTH_WEEK_HOUR_MIN) {

            LayoutInflater.from(context).inflate(R.layout.pickerview_week, contentContainer);

        }else if (type == Type.YEAR_MONTH){

            LayoutInflater.from(context).inflate(R.layout.pickerview_time_ym, contentContainer);

        }else {
            LayoutInflater.from(context).inflate(R.layout.pickerview_time, contentContainer);
        }
        // -----确定和取消按钮
        btnSubmit = findViewById(R.id.btnSubmit);
        btnSubmit.setTag(TAG_SUBMIT);
        btnCancel = findViewById(R.id.btnCancel);
        btnCancel.setTag(TAG_CANCEL);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        //顶部标题
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        // ----时间转轮
        final View timepickerview = findViewById(R.id.timepicker);
        wheelTime = new WheelTime(timepickerview, type);
        wheelOptions = new WheelOptions(timepickerview);
        wheelOptions.setOnTimePeriodChangeListener(this);

        //默认选中当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        wheelTime.setPicker(year, month, day, hours, minute);

    }

    public void setPicker(ArrayList<T> optionsItems) {
        wheelOptions.setPicker(optionsItems, null, null, false);
    }

    public void setPicker(ArrayList<T> options1Items,
                          ArrayList<ArrayList<T>> options2Items, boolean linkage) {
        wheelOptions.setPicker(options1Items, options2Items, null, linkage);
    }

    public void setPicker(ArrayList<T> options1Items,
                          ArrayList<ArrayList<T>> options2Items,
                          ArrayList<ArrayList<ArrayList<T>>> options3Items,
                          boolean linkage) {
        wheelOptions.setMaSegementPicker(options1Items, options2Items, options3Items,
                linkage);
    }
    /**
     * 设置选中的item位置
     * @param option1
     */
    public void setSelectOptions(int option1){
        wheelOptions.setCurrentItems(option1, 0, 0);
    }
    /**
     * 设置选中的item位置
     * @param option1
     * @param option2
     */
    public void setSelectOptions(int option1, int option2){
        wheelOptions.setCurrentItems(option1, option2, 0);
    }
    /**
     * 设置选中的item位置
     * @param option1
     * @param option2
     * @param option3
     */
    public void setSelectOptions(int option1, int option2, int option3){
        wheelOptions.setCurrentItems(option1, option2, option3);
    }

    /**
     * 设置可以选择的时间范围
     *
     * @param startYear
     * @param endYear
     */
    public void setRange(int startYear, int endYear) {
        wheelTime.setStartYear(startYear);
        wheelTime.setEndYear(endYear);
    }

    /**
     * 设置选中时间
     * @param date
     */
    public void setTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date == null)
            calendar.setTimeInMillis(System.currentTimeMillis());
        else
            calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        wheelTime.setPicker(year, month, day, hours, minute);
    }

    public void setTimeWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date == null)
            calendar.setTimeInMillis(System.currentTimeMillis());
        else
            calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        wheelTime.setPicker(year, month, day, hours, minute,week);
    }

//    /**
//     * 指定选中的时间，显示选择器
//     *
//     * @param date
//     */
//    public void show(Date date) {
//        Calendar calendar = Calendar.getInstance();
//        if (date == null)
//            calendar.setTimeInMillis(System.currentTimeMillis());
//        else
//            calendar.setTime(date);
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH);
//        int day = calendar.get(Calendar.DAY_OF_MONTH);
//        int hours = calendar.get(Calendar.HOUR_OF_DAY);
//        int minute = calendar.get(Calendar.MINUTE);
//        wheelTime.setPicker(year, month, day, hours, minute);
//        show();
//    }

    /**
     * 设置是否循环滚动
     *
     * @param cyclic
     */
    public void setCyclic(boolean cyclic) {
        wheelTime.setCyclic(cyclic);
    }

    public void setMaCyclic(boolean cyclic) {
        wheelTime.setCyclic(cyclic);
        wheelOptions.setCyclic(false,false,false);

    }

    @Override
    public void onClick(View v) {
        String tag = (String) v.getTag();
        if (tag.equals(TAG_CANCEL)) {
            dismiss();
            return;
        } else {
            if (timeSelectListener != null || optionsSelectListener != null) {
                try {
                    Date date = null;
                    //Date date = WheelTime.dateFormat.parse(wheelTime.getTime());
                    if (mType == Type.MONTH_WEEK_HOUR_MIN) {

                        date = WheelTime.dateFormat.parse(wheelTime.getWeekTime());

                        int[] optionsCurrentItems = wheelOptions.getCurrentItems();

                        optionsSelectListener.onOptionsSelect(date,optionsCurrentItems[0], optionsCurrentItems[1], optionsCurrentItems[2]);

                    }else {

                        date = WheelTime.dateFormat.parse(wheelTime.getTime());

                        timeSelectListener.onTimeSelect(date);
                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            dismiss();
            return;
        }
    }

    public interface onTimePeriodChangeListener{

        void timePeriodChange(String text);
    }

    public void setOnTimePeriodChangeListener(onTimePeriodChangeListener onTimePeriodChangeListener)
    {

        this.timePeriodChangeListener = onTimePeriodChangeListener;
    }

    public interface OnTimeSelectListener {
        public void onTimeSelect(Date date);
    }

    public void setOnTimeSelectListener(OnTimeSelectListener timeSelectListener) {
        this.timeSelectListener = timeSelectListener;
    }

    public interface OnOptionsSelectListener {
        public void onOptionsSelect(Date date, int options1, int option2, int options3);
    }

    public void setOnoptionsSelectListener(
            OnOptionsSelectListener optionsSelectListener) {
        this.optionsSelectListener = optionsSelectListener;
    }


    public void setTitle(String title){
        tvTitle.setText(title);
    }
}
